package com.backpackerxl.blog.web.admin;

import com.backpackerxl.blog.service.CommentService;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * Author by：Backpackersxl
 * Date: 2021/5/14/014
 * Time: 21:47
 **/
@Controller
@RequestMapping("/admin")
public class ComentGroupController {
    private final CommentService commentService;

    public ComentGroupController(CommentService commentService) {
        this.commentService = commentService;
    }

    @GetMapping("/groupcoment")
    public String groupComments(@PageableDefault(size = 5, sort = {"id"}, direction = Sort.Direction.DESC) Pageable pageable,
                                Model model) {
        model.addAttribute("page", commentService.findAll(pageable));
        return "admin/comments";
    }

    @GetMapping("/comments/{id}/delete")
    public String deleteTag(@PathVariable Long id, RedirectAttributes attributes) {
        commentService.deleteComment(id);
        attributes.addFlashAttribute("message", "删除成功");
        return "redirect:/admin/groupcoment";
    }

}
